package com.eastelsoft.smartiot.bssserver.controller;

import com.eastelsoft.smartiot.bssserver.dao.*;
import com.eastelsoft.smartiot.bssserver.domain.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* 数据库 [okwuyou_crawler] 中表 [T_CM_ORDER] 的Controller类.
*  
* @author: {GenerateCode}
* 创建日期: 2019/9/4
* 修 改 人: 
* 修改日期:
* 修改内容:
* 版    本: 1.0.0
*/
@SuppressWarnings("ALL")
@RestController
@RequestMapping("/api/goldsandalwood/tcmorder")
public class TCmOrderController extends AbstractController {
    private static Logger logger = LoggerFactory.getLogger(TCmOrderController.class);
     @Autowired
    private TCmOrderMapper tCmOrderMapper;
    @Autowired
    private TCmAppAuthorizeMapper tCmAppAuthorizeMapper;
    @Autowired
    private TCmUserAccountMapper tCmUserAccountMapper;


    /**
     * 列表
     */
    @CrossOrigin(origins = "*")
    @RequestMapping(value = "/listTCmOrder", method = RequestMethod.GET)
    public PageInfo getTCmOrderByIdListByPage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(defaultValue = "default") String openId,
     HttpServletRequest request, HttpServletResponse response) {
        PageHelper.startPage(page,size);
        PageInfo pageInfo =null;
        if (openId.equals("default")) {
          logger.error("openId为空，不能获取订单信息");
        }else {
            TCmAppAuthorize tCmAppAuthorize = (TCmAppAuthorize) tCmAppAuthorizeMapper.selectByOpenId(openId);
            TCmUserAccount tCmUserAccount=tCmUserAccountMapper.selectByPrimaryKey(tCmAppAuthorize.getUserAccountId());
           // 用户类型是商家的时候，返回该用户名下的订单信息。管理员和员工，返回所有的订单信息
            if(tCmUserAccount.getUserType()==1) {
                pageInfo = new PageInfo(tCmOrderMapper.selectByCusId(tCmUserAccount.getOperateId()));
            }else{
                pageInfo = new PageInfo(tCmOrderMapper.selectAll());
            }
        }
        logger.info("******PageSize="+String.valueOf(pageInfo.getPageSize()));
        return pageInfo;
    }
    @CrossOrigin(origins = "*")
    @RequestMapping(value = "/getTCmOrderItemByOpenIdIdAndOrderStatus", method = RequestMethod.GET)
    public PageInfo getTCmOrderItemByOpenIdIdAndOrderStatus( @RequestParam(defaultValue = "1") int page,
                                                             @RequestParam(defaultValue = "10") int size,
            @RequestParam String orderItemId, @RequestParam(defaultValue = "default") String openId ,@RequestParam int orderStatus ) {
        PageHelper.startPage(page,size);
        PageInfo pageInfo =null;
        if (openId.equals("default")) {
            logger.error("openId为空，不能获取订单信息");
        }else {
            TCmAppAuthorize tCmAppAuthorize = (TCmAppAuthorize) tCmAppAuthorizeMapper.selectByOpenId(openId);
            TCmUserAccount tCmUserAccount = tCmUserAccountMapper.selectByPrimaryKey(tCmAppAuthorize.getUserAccountId());
            String cusId = tCmUserAccount.getOperateId();
            pageInfo = new PageInfo(tCmOrderMapper.selectBycusIdAndOrderStatus(orderItemId, cusId, orderStatus));
        }
        return pageInfo;
    }
    @CrossOrigin(origins = "*")
    @RequestMapping(value = "/getTCmOrderById", method = RequestMethod.GET)
    public TCmOrder getTCmOrderById(@RequestParam String orderId) {
        TCmOrder pojo = tCmOrderMapper.selectByPrimaryKey(orderId);
        if (pojo == null) {
            return null;
        }
        return pojo;
    }
    @CrossOrigin(origins = "*")
    @RequestMapping(value = "/insertTCmOrder", method = RequestMethod.POST)
    public Boolean insert(@RequestBody TCmOrder param) {
        int x= tCmOrderMapper.insert(param);
        return x > 0;
    }
    @CrossOrigin(origins = "*")
    @RequestMapping(value = "/updateTCmOrder", method = RequestMethod.POST)
    public Boolean update(@RequestBody TCmOrder param) {
        int x= tCmOrderMapper.updateByPrimaryKey(param);
        return x > 0;
    }

    /**
     * 采用物理删除 delFlag
     * @param param
     * @return
     */
    @CrossOrigin(origins = "*")
    @RequestMapping(value = "/deleteTCmOrder", method = RequestMethod.POST)
    public Boolean delete(@RequestBody TCmOrder param) {
        short delflag=1;
        param.setDelFlag(delflag);
        int x= tCmOrderMapper.updateByPrimaryKey(param);
        return x > 0;
    }
    // TODO add method
}